Skip to content

fix: use math.fsum in variance to avoid fp rounding when all credits are equal - #10

Merged
dcondrey merged 1 commit into
mainfrom
fix/partial-credit-variance
Jul 27, 2026
Merged

fix: use math.fsum in variance to avoid fp rounding when all credits are equal#10
dcondrey merged 1 commit into
mainfrom
fix/partial-credit-variance

Conversation

@dcondrey

Copy link
Copy Markdown
Owner

Summary

  • PartialCreditScore.variance was using sum() to compute the mean, which accumulates floating-point rounding error
  • When all 8 credits are identical (e.g. 0.8), sum([0.8]*8)/8 is not exactly 0.8, so (c - m)**2 is not exactly 0.0, yielding a residual of ~1.2e-32 instead of 0.0
  • Switching to math.fsum (exact summation) for both the mean and the squared-deviations sum eliminates the error

Root cause

0.8 is not exactly representable in IEEE 754; sum() accumulates that error across 8 terms. math.fsum uses an extended-precision accumulator that cancels the error exactly.

Test plan

  • pytest tests/test_partial_credit.py passes locally (8/8 green)
  • CI passes on all matrix legs (py3.10–3.13 / ubuntu + macos)

@dcondrey
dcondrey merged commit 41b62b9 into main Jul 27, 2026
9 checks passed
@dcondrey
dcondrey deleted the fix/partial-credit-variance branch July 27, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant